#include "mainwindow.h"
#include <qheaderview.h>
#include <qdebug.h>

void MainWindow::initWindow() {
	this->resize(800, 400);
	ui.wCenter = new QWidget(nullptr);
	this->setCentralWidget(ui.wCenter);
	auto& w = ui.wCenter;

	qDebug() << "initWindow" << (w == nullptr) << (w == this->centralWidget());
	ui.topLO = new QHBoxLayout(w);
	ui.lFrame = new QFrame(w);
	ui.lFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
	ui.topLO->addWidget(ui.lFrame);
	ui.midFrame = new QFrame(w);
	ui.midFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
	ui.topLO->addWidget(ui.midFrame);
	ui.rFrame = new QFrame(w);
	ui.rFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
	ui.topLO->addWidget(ui.rFrame);

	ui.rFrameLO = new QVBoxLayout(ui.rFrame);
	ui.lFrameLO = new QVBoxLayout(ui.lFrame);
	ui.midFrameLO = new QVBoxLayout(ui.midFrame);

	ui.lblInit = new QLabel(QStringLiteral("initialization"), ui.lFrame);
	ui.lFrameLO->addWidget(ui.lblInit);

	ui.btnInit = new QPushButton(QStringLiteral("CAN Init"), ui.rFrame);
	ui.lFrameLO->addWidget(ui.btnInit);

	ui.lblSetting = new QLabel(QStringLiteral("Setting"), ui.lFrame);
	ui.lFrameLO->addWidget(ui.lblSetting);

	ui.motorPairLO = new QHBoxLayout(ui.lFrame);
	ui.lFrameLO->addLayout(ui.motorPairLO);
	ui.rotatePairLO = new QHBoxLayout(ui.lFrame);
	ui.lFrameLO->addLayout(ui.rotatePairLO);

	ui.spMotorPair = new QSpinBox(ui.lFrame);
	ui.spMotorPair->setValue(3);
	ui.lblMotorPair = new QLabel(QStringLiteral("Pairs of Motor"), ui.lFrame);

	ui.motorPairLO->addWidget(ui.lblMotorPair);
	ui.motorPairLO->addWidget(ui.spMotorPair);

	ui.spRotatePair = new QSpinBox(ui.lFrame);
	ui.spRotatePair->setValue(3);
	ui.lblRotatePair = new QLabel(QStringLiteral("Pairs of Rotation"), ui.lFrame);

	ui.rotatePairLO->addWidget(ui.lblRotatePair);
	ui.rotatePairLO->addWidget(ui.spRotatePair);

	ui.lblStart = new QLabel(QStringLiteral("Start"), ui.lFrame);
	ui.lFrameLO->addWidget(ui.lblStart);

	ui.btnStart = new QPushButton(QStringLiteral("Start"), ui.rFrame);
	ui.lFrameLO->addWidget(ui.btnStart);

	ui.table = new QTableWidget(ui.midFrame);
	ui.table->setColumnCount(2);
	ui.midFrameLO->addWidget(ui.table);
	// set the vertical header text:
	ui.table->verticalHeader()->setVisible(false);
	ui.table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	ui.table->setHorizontalHeaderLabels(QStringList() << QStringLiteral("SN") << QStringLiteral("Angle"));
	ui.table->setColumnCount(2);

	ui.lblExport = new QLabel(QStringLiteral("Data Export"), ui.rFrame);
	ui.rFrameLO->addWidget(ui.lblExport);

	ui.lblPath = new QLabel(QStringLiteral("Please select the path to save file"), ui.rFrame);
	ui.rFrameLO->addWidget(ui.lblPath);

	ui.exportLO = new QHBoxLayout(ui.rFrame);
	ui.rFrameLO->addLayout(ui.exportLO);
	ui.btnSelectPath = new QPushButton(QStringLiteral("Select path for saving file"), ui.rFrame);
	ui.exportLO->addWidget(ui.btnSelectPath);
	ui.btnExport = new QPushButton(QStringLiteral("Export"), ui.rFrame);
	ui.exportLO->addWidget(ui.btnExport);

	ui.lblShowAngle = new QLabel(QStringLiteral("Angle"), ui.rFrame);
	ui.rFrameLO->addWidget(ui.lblShowAngle);

	ui.dspAngle = new QDoubleSpinBox(ui.rFrame);
	ui.dspAngle->setRange(0, 360);
	ui.dspAngle->setSingleStep(0.1);
	ui.dspAngle->setDecimals(1);
	ui.dspAngle->setValue(0);
	ui.rFrameLO->addWidget(ui.dspAngle);

	ui.btnStop = new QPushButton(QStringLiteral("Stop"), ui.rFrame);
	ui.rFrameLO->addWidget(ui.btnStop);

	ui.lblMsg = new QLabel(QStringLiteral("Message"), ui.rFrame);
	ui.rFrameLO->addWidget(ui.lblMsg);

	ui.topLO->setStretch(0, 1);
	ui.topLO->setStretch(1, 2);
	ui.topLO->setStretch(2, 1);

	// signals:
	connect(ui.btnSelectPath, &QPushButton::clicked, this, &MainWindow::onBtnSelectPathClicked);
	connect(ui.btnInit, &QPushButton::clicked, this, &MainWindow::onBtnInitClicked);
	connect(ui.btnStart, &QPushButton::clicked, this, &MainWindow::onBtnStartClicked);
	connect(ui.btnExport, &QPushButton::clicked, this, &MainWindow::onBtnExportClicked);
	//connect(ui.btnSave, &QPushButton::clicked, this, &MainWindow::onBtnSaveClicked);

	connect(ui.btnStop, &QPushButton::clicked, this, &MainWindow::onBtnStopClicked);

	ui.btnStart->setEnabled(false);
}